package com.hmall.item.web;

import com.hmall.common.dto.PageDTO;
import com.hmall.item.interceptor.BaseContext;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import org.apache.http.HttpRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("item")
public class ItemController {

    @Autowired
    private IItemService itemService;


    @GetMapping("/list")
    public PageDTO<Item> findPage(@RequestParam(value = "page")Long page, @RequestParam(value = "size")Long size,HttpServletRequest request){
        System.out.println("request.getHeader = " + request.getHeader("authorization"));
        return itemService.findPage(page,size);
    }

    @PostMapping
    public void addItem(@RequestBody Item item){
        itemService.addItem(item);
    }

    @PutMapping("/status/{id}/{sta}")
    public void updateStatus(@PathVariable("id")Long id, @PathVariable("sta")Long sta)
    {
        String currentID = BaseContext.getCurrentID();
        System.out.println("currentID = " + currentID);
        itemService.updateStatus(id,sta);
    }

    @PutMapping
    public void updateInfo(@RequestBody Item item){
        itemService.updateById(item);
    }

    @DeleteMapping("/{id}")
    public void deleteById(@PathVariable("id")Long id){
        itemService.deleteById(id);
    }
}
